;(function() {

    var DateField = function(element, date, noday, render) {
        this.element = element;
        this.showtarget = element.find('.com_list_table_rcell');
        this.date = date;
        this.noday = noday ? 2 : 1;
        if(render) {
            this.render = render;
        }
        this.init();
    }

    DateField.prototype.init = function() {
        var self = this;
        this.element.unbind('tap');
        this.element.bind('tap',function(index) {
            self._init();//点击日历控件触发一个事件，用来重写
            if(DateField._loading || !DateField._loaded) {
                DateField._loading = true;
                // 创建城市外层标签
                var wrap = $(document.createElement('div'));
                if(!App.IS_ANDROID) {
                    wrap.addClass('com_box_panelwrap');
                }
                $(document.body).append(wrap);
                if(AheadHandle.loadingBee&&AheadHandle.loadingBee.canvas){
		    		AheadHandle.loadingBee.show();
		    	}
                wrap.load(App.tplUrls.date, _.bind(function() {
                    this._load(function() {
                        App.focusElement.style.display = 'block';
                        App.DateBox.show(self.date, _.bind(self.setDate, self), self.noday);
                        if(AheadHandle.loadingBee&&AheadHandle.loadingBee.canvas){
				    		AheadHandle.loadingBee.hide();
				    	}
                    });
                }, self));
            } else {
                //App.focusElement.style.display = 'block';
                App.DateBox.show(self.date, _.bind(self.setDate, self), self.noday);
            }
        });

        this.render();
    }

    DateField.prototype._load = function(callback) {
        loadJs([App.completeUrls.touch, //
        App.completeUrls.scroll, //
        App.completeUrls.box], _.bind(function(index) {
            if(index == 3) {
                // 加载完毕
                DateField._loading = false;
                DateField._loaded = true;
                callback();
            }
        }, this), true);
    }
    /**
     * 渲染日期, 允许重构
     */
    DateField.prototype.render = function(onlyget) {
        //日历控件完成触发一个事件，用来重写
        this._render();
        var date = this.date;
        if(date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            month = (month < 10 ? '0' + month : month);
            day = (day < 10 ? '0' + day : day);

            var format = year + '-' + month + '-' + day;
            if(!onlyget) {
            	if(this.showtarget.is('input')){//stz+ input type 20130627
            		this.showtarget.val(format);
            	}else{
            		this.showtarget.text(format);
            	}                
            }
            return format;
        }
    }
    
    //日历控件完成触发一个事件，用来重写
    DateField.prototype._render=function(){
        //日历控件出现穿透的情况，此处修正
        setTimeout(function(){
            $('select').removeAttr('disabled');
            $('input').removeAttr('disabled');
        },300)  
    }
    //点击日历控件触发一个事件，用来重写
    DateField.prototype._init=function(){
         //日历控件出现穿透的情况，此处修正
         $('select').attr('disabled','disabled');
         $('input').attr('disabled','disabled');  
    }

    DateField.prototype.setElement = function(element) {
        this.element = element;
    }

    DateField.prototype.setDate = function(date, hide) {
        this.date = date;
        if(this.validate(date)) {
            this.render();
            if(hide) {
                App.DateBox.hide();
                setTimeout(function() {
                    App.focusElement.style.display = 'none';
                }, 500);
            }
        }
    }

    DateField.prototype.getDate = function() {
        return this.date;
    }

    DateField.prototype.validate = function() {
        return true;
    }
    //只做显示，不可点击操作
    DateField.prototype.setDisabled = function(){
        this.element.unbind('tap');
    }
    
    //取消不可点击的操作
    DateField.prototype.removeDisabled = function(){
        this.init();
    }

    App.DateField = DateField;
})();
